package src.jbdc4;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import src.德鲁伊数据库连接池.Brand;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

public class jdbc4 {
    private Connection connection;

    public jdbc4() throws Exception {
        this.conn();
    }
    //封装德鲁伊方法
    public void conn() throws Exception {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/德鲁伊数据库连接池/DruidJdbc.properties");
        properties.load(fileInputStream);
        //使用德鲁伊
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        this.connection = dataSource.getConnection();
    }

    //定义查询方法
    public ArrayList<Brand> findAllDate(String sql) throws SQLException, SQLException {
        ArrayList<Brand> brand =null;
        //预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()){
            Brand brands = new Brand();
            brands.setId(resultSet.getInt("id"));
            brands.setUsername(resultSet.getString("username"));
            brands.setPassword(resultSet.getString("password"));
            brand.add(brands);
        }
        return brand;
    }
    //定义增加方法‘
    public int insert(Brand brand) throws SQLException {
        //定义sql语句
        String sql="insert into tbl_lixue values(?,?,null)";
        //预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        //设计？的值
        statement.setString(1,brand.getUsername());
        statement.setString(2,brand.getPassword());
        int i = statement.executeUpdate();
        return  i;
    }
}
